package partner_urban

import (
	"gitee.com/theegg/go-brick/app/model/system/system_districts"
	"gitee.com/theegg/go-brick/app/model/system/system_images"
	"gitee.com/theegg/go-brick/app/model/urban/urban_elements"
	"gitee.com/theegg/go-brick/app/service/app"
	"gitee.com/theegg/go-brick/app/service/help/cstruct"
	"github.com/gin-gonic/gin"
	"strconv"
)

// @title GetElementConvenientList
// @description 获取餐厅列表数据
func GetElementConvenientList(c *gin.Context) {
	var mainType = 5
	// 检验参数
	var validaParams urban_elements.QueryParams
	if err := c.ShouldBind(&validaParams); err != nil {
		app.NewResponse(c).JsonReturn(false, err.Error(), nil)
		return
	}

	// 查询数据
	validaParams.TypeId = mainType
	data, err := urban_elements.GetDataList(cstruct.StructToMap(validaParams))
	if err != nil {
		app.NewResponse(c).JsonReturn(false, err.Error(), data)
		return
	}

	typeOptions, errType := urban_elements.GetElementTypeSelectOptions(mainType)
	if errType != nil {
		app.NewResponse(c).JsonReturn(false, errType.Error(), data)
		return
	}

	data["type_options"] = typeOptions

	districtData, errDis := system_districts.GetDistrictSortOptionByCountry("cn")
	if errDis != nil {
		app.NewResponse(c).JsonReturn(false, errDis.Error(), data)
		return
	}

	data["district_options"] = districtData
	data["country_options"] = system_districts.DistrictCountry
	app.NewResponse(c).JsonReturn(true, "获取数据成功", data)
	return
}

// @title ElementConvenientAdd
// @description 新增餐厅
func ElementConvenientAdd(c *gin.Context) {
	idStr := c.PostForm("id")
	id, err := strconv.ParseInt(idStr, 10, 64)
	if err != nil {
		app.NewResponse(c).JsonReturn(false, err.Error(), nil)
		return
	}

	var teModel *urban_elements.UrbanElements
	var errGet error
	if id > 0 {
		teModel, errGet = urban_elements.GetItemById(id)
		if errGet != nil {
			app.NewResponse(c).JsonReturn(false, errGet.Error(), nil)
			return
		}
	} else {
		teModel = new(urban_elements.UrbanElements)
	}

	// 校验参数
	var validaParams urban_elements.AddParams
	if err := c.ShouldBind(&validaParams); err != nil {
		app.NewResponse(c).JsonReturn(false, "bind-json-"+err.Error(), nil)
		return
	}

	// 餐厅默认为 5
	validaParams.TypeId = 5

	errAu := teModel.AddOrUpdate(validaParams)
	if errAu != nil {
		app.NewResponse(c).JsonReturn(false, errAu.Error(), nil)
		return
	}

	app.NewResponse(c).JsonReturn(true, "获取数据成功", nil)
	return
}

// @title ElementRestaurantStatus
// @description 修改餐厅状态
func ElementConvenientStatus(c *gin.Context) {
	teModel, err := getTargetElementConvenientFromParams(c)
	if err != nil {
		app.NewResponse(c).JsonReturn(false, err.Error(), nil)
		return
	}

	errPublish := teModel.ModifyStatus()
	if errPublish != nil {
		app.NewResponse(c).JsonReturn(false, errPublish.Error(), nil)
		return
	}

	app.NewResponse(c).JsonReturn(true, "恭喜, 上架成功", nil)
	return
}

// @title ElementRestaurantImageUpload
// @description 图片上传 cover / content
func ElementConvenientImageUpload(c *gin.Context) {
	imgType := c.PostForm("type")
	if imgType != "cover" {
		imgType = "content"
	}

	imageName, errUpload := system_images.SaveImage("tour", imgType, c)
	if errUpload != nil {
		app.NewResponse(c).JsonReturn(false, errUpload.Error(), nil)
		return
	}

	returnData := make(map[string]interface{})
	returnData["file_url"] = app.GetImageStaticUrl(imageName)
	returnData["file_name"] = imageName

	app.NewResponse(c).JsonReturn(true, "上传图片成功!", returnData)
	return
}

/** =========================== 辅助查询 ============================ **/
// @title getTargetElementRestaurantFromParams
// @description 内置方法　通过参数id获取目标对象
func getTargetElementConvenientFromParams(c *gin.Context) (*urban_elements.UrbanElements, error) {
	id := c.PostForm("id")
	if id == "" {
		return nil, app.NewError("对不起, 管理员ID不能为空")
	}

	// ID 转换格式
	int64Id, errParse := strconv.ParseInt(id, 10, 64)
	if errParse != nil {
		return nil, app.NewError("对不起, 目标用户ID格式不正确")
	}

	// 获取对象
	item, errTarget := urban_elements.GetItemById(int64Id)
	if errTarget != nil {
		return nil, app.NewError(errTarget)
	}

	// 目标需要存在
	if item == nil {
		return nil, app.NewError("对不起, 目标不存在")
	}

	return item, nil
}
